package com.csun.cmny.provider.controller;


import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.constant.ServiceOrderComplaintConstant;
import com.csun.cmny.provider.model.domain.OmcLongRangeServiceComplaint;
import com.csun.cmny.provider.model.domain.OmcLongRangeServiceOrder;
import com.csun.cmny.provider.model.domain.OmcServiceOrder;
import com.csun.cmny.provider.model.dto.comolaint.ServiceComplaintSaveDto;
import com.csun.cmny.provider.model.dto.comolaint.ServiceComplaintSelectDto;
import com.csun.cmny.provider.model.dto.longRangeServiceOrder.LongRangeServiceComplaintSaveDto;
import com.csun.cmny.provider.model.dto.longRangeServiceOrder.LongRangeServiceComplaintSelectDto;
import com.csun.cmny.provider.model.vo.complaint.ServiceComplaintVo;
import com.csun.cmny.provider.model.vo.longRangeServiceOrder.LongRangeServiceComplaintVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcLongRangeServiceComplaintService;
import com.csun.cmny.provider.service.OmcLongRangeServiceOrderService;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "/longRangeServiceComplaint", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcLongRangeServiceComplaintController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private OmcLongRangeServiceOrderService longRangeServiceOrderService;

    @Resource
    private OmcLongRangeServiceComplaintService complaintService;

    /*
     * 提交投诉
     * */
    @PostMapping(value = "/submit")
    public Wrapper submit (@RequestBody LongRangeServiceComplaintSaveDto serviceComplaintSaveDto) {


        OmcLongRangeServiceOrder longRangeServiceOrder = longRangeServiceOrderService.selectByKey(serviceComplaintSaveDto.getServiceOrderId());

        //校验订单是否存在
        if (PublicUtil.isEmpty(longRangeServiceOrder)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("service.order.error", null));
        }

        //校验是否本人订单
        if (!longRangeServiceOrder.getUserId().equals(getLoginAuthDto().getUserId())){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("service.complaint.notMy", null));
        }

        //校验此订单是否已经投诉过
        if (longRangeServiceOrder.getComplaintStatus()!= ServiceOrderComplaintConstant.ORDER_COMPLAINT_NULL){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("service.complaint.save.error", null));
        }


        int result=complaintService.submit(serviceComplaintSaveDto);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),result);
    }

    /*
     * 查看投诉
     * */
    @PostMapping(value = "/select")
    public Wrapper select (@RequestBody LongRangeServiceComplaintSelectDto longRangeServiceComplaintSelectDto) {


        List<LongRangeServiceComplaintVo> serviceComplaintVos=complaintService.selectComplaint(longRangeServiceComplaintSelectDto);


        PageInfo<LongRangeServiceComplaintVo> pageInfo = new PageInfo<>(serviceComplaintVos);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),pageInfo);
    }

    /*
     * 处理投诉
     * */
    @PostMapping(value = "/dispose")
    public Wrapper dispose (Long serviceOrderId) {


        //订单是否存在
        OmcLongRangeServiceOrder longRangeServiceOrder = longRangeServiceOrderService.selectByKey(serviceOrderId);
        if (PublicUtil.isEmpty(longRangeServiceOrder)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("complaint.serviceOrder.isnull", null));
        }

        //此投诉不存在
        if (longRangeServiceOrder.getComplaintStatus()==ServiceOrderComplaintConstant.ORDER_COMPLAINT_NULL){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("complaint.is.null", null));
        }
        OmcLongRangeServiceComplaint omcLongRangeServiceComplaint = complaintService.selectByKey(serviceOrderId);
        if (PublicUtil.isEmpty(omcLongRangeServiceComplaint)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("complaint.is.null", null));
        }
        //此投诉已经处理
        if (longRangeServiceOrder.getComplaintStatus()==ServiceOrderComplaintConstant.ORDER_COMPLAINT_RECEIVE){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("complaint.dispose.error", null));
        }

        longRangeServiceOrder.setComplaintStatus(ServiceOrderComplaintConstant.ORDER_COMPLAINT_RECEIVE);

        int result = longRangeServiceOrderService.update(longRangeServiceOrder);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),result);
    }
}
